﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using UWP.Douban.Api;
using UWP.Douban.Repositories;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace UWP.Douban.Mvvm
{
    public class MainVm:BaseVm
    {
        private Scenario _currentScenario;
        private ConfigRepository _configRepository;

        public MainVm()
        {
            TopScenarios = new List<Scenario>();
            BottomScenarios = new List<Scenario>();
        }

        public MainVm(ConfigRepository configRepository):this()
        {
            _configRepository = configRepository;
            LoadScenarios();
        }
        

        public List<Scenario> TopScenarios { get; set; }

        public List<Scenario> BottomScenarios { get; set; }

        public Scenario CurrentScenario
        {
            get
            {
                return _currentScenario;
            }
            set
            {
                if (_currentScenario == value) return;
                _currentScenario = value;

                OnPropertyChanged();
            }
        }


        public override async void OnNavigatedTo(NavigationEventArgs e)
        {

            var pageShortTitle = "";
            base.OnNavigatedTo(e);

            //读取Token
            var authenticate = _configRepository.GetAuthenticate();
            if(authenticate != null)
            {
                authenticate.SetCommon();

                var user = await GroupApi.UserInfoGet(authenticate.UserId);

                if (user != null)
                {
                    //进入我的话题页面
                    pageShortTitle = "话题";
                    GoDefaultPage(pageShortTitle);
                    return;
                }
            }

            //进入登陆界面
            pageShortTitle = "登陆";


            LoadScenariosNoLogined();
            GoDefaultPage(pageShortTitle);
        }

        private void GoDefaultPage(string pageTitle)
        {
            if (CurrentScenario == null)
            {
                CurrentScenario = TopScenarios.Find(o => o.ShortTitle == pageTitle);
            }

            CurrentScenario.GoPageCommand.Execute(ScenarioFrame);
        }

        private void LoadScenarios()
        {
            TopScenarios.Clear();
            TopScenarios = new List<Scenario>() {
                new Scenario() {ShortTitle= "我", Title = "我的信息", ClassType= typeof(Views.MePage), Symbol= Symbol.People},
                new Scenario() {ShortTitle= "话题",Title = "我的话题", ClassType= typeof(Views.MyTopicPage), Symbol= Symbol.Message},
                new Scenario() {ShortTitle= "小组",Title = "我的小组", ClassType= typeof(Views.MyGroupPage), Symbol= Symbol.AllApps    },
                new Scenario() {ShortTitle= "推荐",Title = "推荐话题", ClassType= typeof(Views.AnonymousTopicPage), Symbol= Symbol.ShowResults},
            };

            BottomScenarios.Clear();
            BottomScenarios = new List<Scenario>() {
                  new Scenario() {ShortTitle= "设置",Title = "设置", ClassType= typeof(Views.TopicCommentPage), Symbol= Symbol.Setting},
            };

            foreach (var item in TopScenarios.Union(BottomScenarios))
            {
                item.GoPageChanged += Item_GoPageChanged;
            }
        }

        private void Item_GoPageChanged(object sender, EventArgs e)
        {
            CurrentScenario = sender as Scenario;
        }

        private void LoadScenariosNoLogined()
        {
            TopScenarios.Clear();
            TopScenarios = new List<Scenario>() {
                new Scenario() {ShortTitle= "登陆", Title = "登陆豆瓣", ClassType= typeof(Views.LoginPage), Symbol= Symbol.Account},
                new Scenario() {ShortTitle= "推荐",Title = "推荐话题", ClassType= typeof(Views.AnonymousTopicPage), Symbol= Symbol.ShowResults},
            };


            BottomScenarios.Clear();

            foreach (var item in TopScenarios.Union(BottomScenarios))
            {
                item.GoPageChanged += Item_GoPageChanged;
            }
        }
    }

    public class Scenario:BaseVm
    {
        private string _title;
        private string _shortTitle;
        private Symbol _symbol;
        private Type _classType;

        public Scenario()
        {
            GoPageCommand = new RelayCommand<Object>(GoPage);
        }

        public Symbol Symbol
        {
            get { return _symbol; }
            set
            {

                if (value == _symbol) return;
                _symbol = value;
                OnPropertyChanged();
            }
        }


        public string Title
        {
            get { return _title; }
            set
            {

                if (value == _title) return;
                _title = value;
                OnPropertyChanged();
            }
        }


        public string ShortTitle
        {
            get { return _shortTitle; }
            set
            {

                if (value == _shortTitle) return;
                _shortTitle = value;
                OnPropertyChanged();
            }
        }


        public Type ClassType
        {
            get { return _classType; }
            set
            {

                if (value == _classType) return;
                _classType = value;
                OnPropertyChanged();
            }
        }

        public ICommand GoPageCommand { get; set; }


        private void GoPage(object obj)
        {
            var frame = obj as Frame;

            if (frame.Content == null || frame.Content.GetType() != ClassType)
            {
                frame?.Navigate(ClassType);

                if (GoPageChanged != null)
                {
                    GoPageChanged(this, new EventArgs());
                }
            }
        }

        public event EventHandler GoPageChanged;
    }
}
